/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmEditPayment.java
 *
 * Created on May 23, 2011, 5:49:16 AM
 */

package Account;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author BachDEV
 */
public class frmEditPayment extends javax.swing.JFrame {

    frmDetails details;
    private double amount = 0;
    private boolean flag = true;
    private String message = "";
    private JTable tblCollector;
    public static int x;
    public static int y;

    /** Creates new form frmEditPayment */
     public frmEditPayment(frmDetails details) {
        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(InstantiationException ex){
            ex.printStackTrace();
        }catch(UnsupportedLookAndFeelException ex){
            ex.printStackTrace();
        }catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }catch(IllegalAccessException ex){
            ex.printStackTrace();
        }
        this.details = details;
        initComponents();
        Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
            x = this.getWidth();
            y = this.getHeight();
            this.setLocation((ds.width - this.getWidth()) / 2, (ds.height - this.getHeight()) / 2);
        btnSave.setEnabled(false);
        try {
            setField();
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }

    }
     public void motion() {
        this.setSize(0, 0);
        new Thread(new SizeMonition(this, x, y)).start();
    }
     /////////////////////////////////////////////////////////////////////
     public JTable buildTableCollector() throws Exception {
        JTable table = null;
        Vector row = null;
        Vector column = new Vector();
        column.add("No.");
        column.add("User name");
        column.add("Full name");
        Vector data = new Vector();
        Connection connect = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String sql = "Select userName,FullName from Users where Type=4";
        try {
            connect = Connections.DBConnection.getConnection("eProject", "sa", "123");
            stmt = connect.prepareStatement(sql);
            rs = stmt.executeQuery();
            int i = 1;
            while (rs.next()) {
                row = new Vector();
                row.add(i);
                row.add(rs.getString("userName"));
                row.add(rs.getString("FullName"));
                data.add(row);
                i++;
            }
            table = new JTable(data, column);
            table.addMouseListener(new MouseListener() {

                public void mouseClicked(MouseEvent e) {
                    btnSave.setEnabled(true);
                }

                public void mousePressed(MouseEvent e) {
                }

                public void mouseReleased(MouseEvent e) {
                }

                public void mouseEntered(MouseEvent e) {
                }

                public void mouseExited(MouseEvent e) {
                }
            });
            table.addKeyListener(new KeyListener() {

                public void keyTyped(KeyEvent e) {
                }

                public void keyPressed(KeyEvent e) {
                }

                public void keyReleased(KeyEvent e) {
                    if(tblCollector.getSelectedRow()!=-1){
                         btnSave.setEnabled(true);
                    }
                }
            });
        } catch (Exception ex) {
            throw ex;
        } finally {
            try {
                Connections.DBConnection.closeConnection(connect, stmt, rs);
            } catch (Exception ex) {
                throw ex;
            }
        }
        return table;
    }


    public void setField() throws Exception {
        try{
            lbOrderID.setText((details.ordid + ""));
        txtAmount.setText(details.tblPaymentDetails.getValueAt(details.tblPaymentDetails.getSelectedRow(), 4).toString());
        lbDate.setText(details.tblPaymentDetails.getValueAt(details.tblPaymentDetails.getSelectedRow(), 2).toString());
        tblCollector = buildTableCollector();
        scrCollector.setViewportView(tblCollector);
        }
        catch(Exception ex){
            throw ex;
        }

    }
    //////////////////////////////////////////////////////////////////////////
     public void saveDatabase() throws Exception {
        Connection connect = null;
        PreparedStatement stmt = null;
        String sql = "Update Payment set Amount=?,userName=? where payID=?";
        try {
            connect = Connections.DBConnection.getConnection("eProject", "sa", "123");
            stmt = connect.prepareStatement(sql);
            amount = Double.parseDouble(txtAmount.getText().trim());
            stmt.setDouble(1, amount);
            stmt.setObject(2, tblCollector.getValueAt(tblCollector.getSelectedRow(), 1).toString(), Types.VARCHAR);
            stmt.setObject(3, details.tblPaymentDetails.getValueAt(details.tblPaymentDetails.getSelectedRow(), 0).toString(), Types.VARCHAR);
            stmt.executeUpdate();
            details.tblPaymentDetails = details.buildTablePaymentDetails();
            details.scrPaymentDetails.setViewportView(details.tblPaymentDetails);
            JOptionPane.showMessageDialog(this, "Update successfull!");
        } catch (Exception ex) {
            throw ex;
        }
    }

    public void checkField() {
        flag = true;
        message = "";
        if (txtAmount.getText().trim().equals("")) {
            message += "You have not entered payment amount!\n;";
            flag = false;
        } else {
            try {
                amount = Double.parseDouble(txtAmount.getText().trim());
            } catch (NumberFormatException ex) {
                message += "Payment amount must be numeric!\n";
                flag = false;
            } catch (Exception ex) {
                ex.printStackTrace();
                message += ex.getMessage();
                flag = false;
            }

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnSave = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        scrCollector = new javax.swing.JScrollPane();
        btnCancel = new javax.swing.JButton();
        txtAmount = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        lbOrderID = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lbDate = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Edit the Payment");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Edit the payment"));

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        jLabel2.setText("$");

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jLabel5.setText("Order ID");

        lbOrderID.setText("Order ID");

        jLabel3.setText("Collector");

        lbDate.setText("Date");

        jLabel6.setText("Date");

        jLabel1.setText("Amount");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(28, 28, 28)
                        .addComponent(scrCollector, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel1)
                            .addComponent(jLabel6))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbDate)
                            .addComponent(lbOrderID)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(txtAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(232, 232, 232))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(138, 138, 138)
                        .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lbOrderID))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(lbDate))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrCollector, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                    .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 564, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 390, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        try {
            checkField();
            if (flag) {
                saveDatabase();
                details.mouseTablePaymentClick();
                details.setAlwaysOnTop(true);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, message);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        dispose();
        details.setEnabled(true);
        details.setAlwaysOnTop(true);
}//GEN-LAST:event_btnCancelActionPerformed

    /**
    * @param args the command line arguments
    */


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbDate;
    private javax.swing.JLabel lbOrderID;
    private javax.swing.JScrollPane scrCollector;
    private javax.swing.JTextField txtAmount;
    // End of variables declaration//GEN-END:variables

}
